<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" size="small" v-show="showSearch" label-width="68px">
      <el-form-item label="用户名称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入用户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phonenumber">
        <el-input
          v-model="queryParams.phonenumber"
          placeholder="请输入用户"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户类型" prop="userType">
          <el-select placeholder="请选择用户类型" v-model="queryParams.userType" size="small">
            <el-option label="会员" value="10"></el-option>
            <el-option label="陪诊" value="20"></el-option>
            <el-option label="护工" value="30"></el-option>
            <el-option label="医生" value="40"></el-option>
          </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <span style="margin-left: 5px;font-size: 20px" >账户总额：{{totalAmount}}陪护豆</span>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="accountList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" prop="" width="50" align="center">
        <template slot-scope="scope">
          <span v-text="getIndex(scope.$index)"> </span>
        </template>
      </el-table-column>
      <el-table-column label="用户名称" align="center" prop="nickName" />
      <el-table-column label="类型" align="center" prop="userType" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_type" :value="scope.row.userType"/>
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center" prop="phonenumber" />
      <el-table-column label="账户余额(个)" align="center" prop="accountBalance" :formatter="accountBalanceFormatter"/>
      <el-table-column label="消费金额(个)" align="center" prop="consumeAmount" />
      <el-table-column label="冻结金额(个)" align="center" prop="freezeAmount" />
      <el-table-column label="提现金额(个)" align="center" prop="withdrawAmount" />
      <el-table-column label="操作" align="center"  class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="handleDetail(scope.row)"
            v-hasPermi="['biz:account:edit']"
          >交易明细</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户账户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" maxlength="20" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="账户余额" prop="accountBalance">
          <el-input v-model="form.accountBalance" maxlength="10,2" placeholder="请输入账户余额" />
        </el-form-item>
        <el-form-item label="消费金额" prop="consumeAmount">
          <el-input v-model="form.consumeAmount" maxlength="10,2" placeholder="请输入消费金额" />
        </el-form-item>
        <el-form-item label="冻结金额" prop="freezeAmount">
          <el-input v-model="form.freezeAmount" maxlength="10,2" placeholder="请输入冻结金额" />
        </el-form-item>
        <el-form-item label="提现金额" prop="withdrawAmount">
          <el-input v-model="form.withdrawAmount" maxlength="10,2" placeholder="请输入提现金额" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" maxlength="255" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :loading="submitLoading" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-drawer
      title="交易明细"
      :visible.sync="drawer"
      size="60%"
      :show-close="true"
      append-to-body
    >
      <div style="margin-left: 10px">
        <el-form :model="tradeQueryParams" ref="tradeQueryForm" :inline="true" size="small" v-show="showSearch" label-width="68px">
          <el-form-item label="交易月份" prop="tradeDate">
            <el-date-picker
              v-model="tradeQueryParams.tradeDate"
              type="month"
              value-format="yyyy-MM"
              placeholder="请选择交易月份">
            </el-date-picker>
          </el-form-item>


          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="handleTradeQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" @click="resetTradeQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <el-table v-loading="tradeLoading" :data="tradeList" >
          <el-table-column label="序号" align="center" type="index" />
          <el-table-column label="流水号" align="center" prop="serialNumber" />
          <el-table-column label="交易金额(陪护豆)" align="center" prop="tradeAmount" />
          <el-table-column label="交易说明" align="center" prop="remark" />
          <el-table-column label="交易时间" align="center" prop="tradeTime" />
        </el-table>
        <pagination
          v-show="tradeTotal>0"
          :total="tradeTotal"
          :page.sync="tradeQueryParams.pageNum"
          :limit.sync="tradeQueryParams.pageSize"
          @pagination="getTradeRecordList"
        />
      </div>

    </el-drawer>
  </div>
</template>

<script>
import { listAccount, getAccount, delAccount, addAccount, updateAccount, changeStatus,getAllUserAccountAmount,getTradeRecordList} from "@/api/biz/account";

export default {
  name: "Account",
  dicts:['sys_user_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      tradeLoading:true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      tradeTotal: 0,
      // 用户账户表格数据
      accountList: [],
      tradeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      drawer:false,
      totalAmount:undefined,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: undefined,
        accountBalance: undefined,
        consumeAmount: undefined,
        freezeAmount: undefined,
        withdrawAmount: undefined,
        nickName:undefined,
        userType:undefined,
        phonenumber:undefined,
      },
      // 查询参数
      tradeQueryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: undefined,
        tradeDate:undefined,
      },
      // 表单参数
      form: {},
      submitLoading: false,
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getAllUserAccountAmount();
  },
  methods: {
    accountBalanceFormatter(row){
      return row.accountBalance +row.giveAwayAmount + row.commissionAmount;
    },
    /** 查询用户账户列表 */
    getList() {
      this.loading = true;
      listAccount(this.queryParams).then(response => {
        this.accountList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getAllUserAccountAmount(){
      getAllUserAccountAmount().then(res =>{
        console.log("res",res);
        this.totalAmount = res.data;
      })
    },
    /** 查询交易明细 */
    getTradeRecordList(){
      this.tradeLoading = true;
      getTradeRecordList(this.tradeQueryParams).then(res =>{
        this.tradeList = res.rows;
        this.tradeTotal= res.total;
        this.tradeLoading = false;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    getIndex($index) {
      //表格序号
      return (this.queryParams.pageNum - 1) * this.queryParams.pageSize + $index + 1
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        accountBalance: null,
        consumeAmount: null,
        freezeAmount: null,
        withdrawAmount: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 搜索按钮操作 */
    handleTradeQuery() {
      this.tradeQueryParams.pageNum = 1;
      this.getTradeRecordList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 重置按钮操作 */
    resetTradeQuery() {
      this.resetForm("tradeQueryForm");
      this.handleTradeQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户账户";
    },
    handleChangeStatus(row) {
      let statusText = row.status === 'active' ? '禁用' : '启用'
      let status = row.status === 'active' ? 'inactive' : 'active'
      const ids = [row.id] || this.ids;
      this.$confirm('是否确认' + statusText + '该项数据?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return changeStatus({ids: ids, status: status});
      }).then(() => {
        this.getList();
        this.msgSuccess("操作成功");
      }).catch(function () {
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAccount(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户账户";
      });
    },
    handleDetail(row){
      this.tradeQueryParams.userId = row.userId;
      this.tradeQueryParams.tradeDate = this.parseTime(new Date(), '{y}-{m}');
      this.drawer = true;
      this.tradeLoading = true;
      getTradeRecordList(this.tradeQueryParams).then(res =>{
        this.tradeList = res.rows;
        this.tradeTotal= res.total;
        this.tradeLoading = false;
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.submitLoading = true
          if (this.form.id != null) {
            updateAccount(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.submitLoading = false;
              this.getList();
            }).catch(() => {
              this.submitLoading = false;
            });
          } else {
            addAccount(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.submitLoading = false;
              this.getList();
            }).catch(() => {
              this.submitLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除用户账户编号为"' + ids + '"的数据项？').then(function() {
        return delAccount(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('biz/account/export', {
        ...this.queryParams
      }, `account_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
